/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Core;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import Entities.Location;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import java.io.Serializable;

/**
 *
 * @author Owner
 */
public class LocationWrapper implements Serializable   {

/*    public static void main(String args[]){

        LocationManager locationManager = new LocationManager();

        locationManager.displayLocationsList(locationManager.getLocations());
    }
*/
    public void initialize(){
        
        Session session = null;

        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

        session = sessionFactory.openSession();

        Location location = new Location();      
    }

    public List getLocations(){

        Session session = null;

        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

        session = sessionFactory.openSession();

        String sql = "select * from Location location";

        Query query = session.createSQLQuery(sql);

        session.close();

        return query.list();
    }

    private Location getLocationById(int id){

        Location location = new Location();
        Session session = null;        

        SessionFactory sessionFactory =
                new Configuration().configure().buildSessionFactory();

        session = sessionFactory.openSession();

        String sql = "select * from Location location where Id = " + String.valueOf(id);

        Query query = session.createSQLQuery(sql);
        query.setMaxResults(1);

        location = (Location) query.uniqueResult();

        session.close();
        
        return location;
    }

    public String getName(int id){

        Location location = getLocationById(id);

        return location.getName();
    }
    


    

    static public void displayLocationsList(List list) {

        Iterator iter = list.iterator();
        
        if (!iter.hasNext()) {
            System.out.println("No suppliers to display.");
            return;
        }

        while (iter.hasNext()) {
            Location location = (Location) iter.next();
            String msg = location.getName();
            System.out.println(msg);
        }
    }

    

    public List<Location> getLocations2(){

        List<Location> locations = new ArrayList<Location>();
        Session session = null;

        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

        session = sessionFactory.openSession();

        return locations;
    }

    
}
